package com.ctvit.cch.core.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.cch.core.enums.CreationSourceEnum;
import com.ctvit.cch.core.mapper.CreativeIdeaMapper;
import com.ctvit.cch.core.mapper.HotRecordMapper;
import com.ctvit.cch.core.pojo.domain.CreativeIdea;
import com.ctvit.cch.core.pojo.domain.HotRecord;
import com.ctvit.cch.core.pojo.dto.creative.CreativeIdeaAddDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeIdeaEditDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeIdeaMyDTO;
import com.ctvit.cch.core.pojo.vo.creative.CreativeIdeaGetVO;
import com.ctvit.cch.core.pojo.vo.creative.CreativeIdeaListVO;
import com.ctvit.cch.core.service.ICreativeIdeaService;
import cn.hutool.core.lang.Assert;

/**
 * 灵感创意Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/10/11
 */
@Service
public class CreativeIdeaServiceImpl extends ServiceImpl<CreativeIdeaMapper, CreativeIdea>
        implements ICreativeIdeaService {

    private final HotRecordMapper hotRecordMapper;

    public CreativeIdeaServiceImpl(HotRecordMapper hotRecordMapper) {
        this.hotRecordMapper = hotRecordMapper;
    }

    @Override
    public CreativeIdeaGetVO get(String id) {
        CreativeIdea one = getById(id);
        CreativeIdeaGetVO vo = new CreativeIdeaGetVO();
        BeanUtils.copyProperties(Objects.requireNonNull(one), vo);
        return vo;
    }

    @Override
    public boolean add(CreativeIdeaAddDTO dto) {
        CreativeIdea one = new CreativeIdea();
        BeanUtils.copyProperties(Objects.requireNonNull(dto), one);
        return save(one);
    }

    @Override
    public boolean edit(CreativeIdeaEditDTO dto) {
        CreativeIdea one = new CreativeIdea();
        BeanUtils.copyProperties(Objects.requireNonNull(dto), one);
        return updateById(one);
    }

    @Override
    public List<CreativeIdeaListVO> getMyList(CreativeIdeaMyDTO dto) {
        return baseMapper.getMyList(dto);
    }

    @Override
    public boolean addByRecord(String recordId) {
        // 根据记录ID查询灵感记录
        HotRecord record = hotRecordMapper.selectById(recordId);
        Assert.notNull(record, "灵感记录不存在");

        // 创建创意对象并设置属性
        CreativeIdea idea = new CreativeIdea();
        idea.setTitle(record.getTitle());
        idea.setDescription(record.getRecommendation());
        idea.setSource(CreationSourceEnum.HOT.name());
        idea.setReferenceLinks(
                StringUtils.isNotBlank(record.getLink()) ? Collections.singletonList(record.getLink())
                        : Collections.emptyList());

        // 保存创意
        return save(idea);
    }

}
